package com.example.music.utils;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class SectionItemDecoration extends RecyclerView.ItemDecoration {
    private static final String TAG = "SectionItemDecoration";
    private int mSectionHeight;
    private int mTextSize;
    private Paint mPaint;
    private Rect mBounds = new Rect();
    private SectionCallback mSectionCallback;

    public interface SectionCallback {
        String getSectionHeader(int position);
    }

    public SectionItemDecoration(int sectionHeight, int textSize, SectionCallback callback) {
        mSectionHeight = sectionHeight;
        mTextSize = textSize;
        mSectionCallback = callback;

        mPaint = new Paint();
        mPaint.setColor(Color.LTGRAY); // 设置背景颜色为浅灰色
        mPaint.setTextSize(mTextSize);
        mPaint.setAntiAlias(true);
        mPaint.setTextAlign(Paint.Align.CENTER); // 文本居中对齐
    }
    @Override
    public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        int childCount = parent.getChildCount();
        if (childCount == 0) return;

        // 实现分区头悬停效果
        String prevHeader = "";
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);
            int pos = parent.getChildAdapterPosition(child);
            if (pos == RecyclerView.NO_POSITION) continue;

            String header = mSectionCallback.getSectionHeader(pos);
            if (header.isEmpty()) continue;

            // 检查是否需要绘制悬停头
            boolean isSameAsPrev = header.equals(prevHeader);
            prevHeader = header;

            // 计算位置
            int top = Math.max(parent.getPaddingTop(), child.getTop() - mSectionHeight);
            int bottom = top + mSectionHeight;

            // 悬停效果：当下一个分区头推上来时
            if (i < childCount - 1) {
                View nextChild = parent.getChildAt(i + 1);
                int nextPos = parent.getChildAdapterPosition(nextChild);
                if (nextPos != RecyclerView.NO_POSITION) {
                    String nextHeader = mSectionCallback.getSectionHeader(nextPos);
                    if (!header.equals(nextHeader) && bottom > nextChild.getTop()) {
                        bottom = nextChild.getTop();
                    }
                }
            }

            drawSection(c, header, top, bottom);
        }
    }

    private void drawSection(Canvas canvas, String sectionName, float top, float bottom) {
        // 保存原始颜色
        int originalColor = mPaint.getColor();

        // 1. 绘制背景
        mPaint.setColor(Color.LTGRAY);
        canvas.drawRect(0, top, canvas.getWidth(), bottom, mPaint);

        // 2. 绘制文本（使用高对比度颜色）
        mPaint.setColor(Color.DKGRAY); // 改用深灰色提高可读性
        mPaint.getTextBounds(sectionName, 0, sectionName.length(), mBounds);
        float x = canvas.getWidth() / 2f;
        float y = (bottom + top) / 2f - ((mPaint.descent() + mPaint.ascent()) / 2);
        canvas.drawText(sectionName, x, y, mPaint);

        // 恢复原始颜色
        mPaint.setColor(originalColor);
    }
    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);

        int pos = parent.getChildAdapterPosition(view);
        String sectionName = mSectionCallback.getSectionHeader(pos);
        if (sectionName != null && !sectionName.isEmpty()) {
            outRect.top = mSectionHeight;
        } else {
            outRect.top = 0;
        }
    }
}